package utils

import (
	"encoding/json"
	"fmt"
	"io"
	"server/internal/common/biz"

	"github.com/zeromicro/go-zero/core/logx"
)

func SeeSuccess(w io.Writer, data string) error {
	logx.Debugf("write: %s", data)
	success := biz.Success(data)
	resp, _ := json.Marshal(success)
	//n, err := fmt.Fprintf(w, "data:%s\n\n", resp)
	n, err := fmt.Fprintf(w, "%s", resp)
	if err != nil {
		logx.Errorf("write failed, n:%d, err:%s", n, err.Error())
		return err
	}
	logx.Debugf("write size:%d", n)
	return nil
}

func SseFail(w io.Writer, e *biz.Error) error {
	logx.Debugf("write: %s", e.Msg)
	fail := biz.Fail(e)
	resp, _ := json.Marshal(fail)
	//n, err := fmt.Fprintf(w, "data:%s\n\n", resp)
	n, err := fmt.Fprintf(w, "%s", resp)
	if err != nil {
		logx.Errorf("write failed, n:%d, err:%s", n, err.Error())
		return err
	}
	logx.Debugf("write size:%d", n)
	return nil
}
